package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.AnswerMapper;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.vo.AnswerCount;
import com.hualan.sfzy.service.BaseService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnswerServiceImpl implements BaseService<Answer> {
    @Autowired
    private AnswerMapper answerMapper;

    /*添加*/
    @Override
    public int add(Answer entity) {
        return answerMapper.insert(entity);
    }

    /*编辑*/
    @Override
    public int edit(Answer entity) {
        return answerMapper.update(entity);
    }

    /*删除*/
    @Override
    public int remove(Integer id) {
        return answerMapper.delete(id);
    }

    /*批量删除*/
    @Override
    public int removeBatch(Integer[] ids) {
        return answerMapper.deleteBatch(ids);
    }

    /*查询所有*/
    @Override
    public List<Answer> find() {
        return answerMapper.select(null);
    }

    /*分页查询所有*/
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Answer entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<Answer> list = answerMapper.select(entity);
        PageInfo<Answer> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据id查询单个*/
    @Override
    public Answer findById(Integer id) {
        return answerMapper.selectById(id);
    }

    /*根据id查询单个(多表)*/
    public Answer selectByIdDuo(Integer id) {
        return answerMapper.selectByIdDuo(id);
    }

    /*查询全部(多表) 带条件*/
    public Map<String, Object> selectDuo(Integer pageNum, Integer pageSize, Answer entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<Answer> list = answerMapper.selectDuo(entity);
        PageInfo<Answer> pageInfo = new PageInfo<>(list); // 分页信息对象
        Map<String, Object> map = new HashMap<>();
        // map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }
//    根据用户id查询全部回答
    public List<Answer> selectUserid(Integer userId) {
        return  answerMapper.selectByuserId(userId);
    }

}
